package com.authine.cloudpivot.common.extension.customized.kafka;

import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.common.extension.customized.service.EmpRelatedInfoSyncService;
import com.yundasys.hrcenter.dto.EmpRelatedInfoRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author saiwen
 * @date 2021/5/23 18:04
 * description 人员数据消费
 */
@Component
@Slf4j
public class EmployeeConsumer {

    @Autowired
    private EmpRelatedInfoSyncService employeeSyncService;

    @KafkaListener(topics = {"HR_EMP_TOPIC"})
    public void listen(ConsumerRecord<String, String> consumer){
        Optional<String> kafkaMsg = Optional.ofNullable(consumer.value());
        if (kafkaMsg.isPresent()){
            String empRelatedInfoRespStr = kafkaMsg.get();
            log.info("kafka增量员工数据为：{}", empRelatedInfoRespStr);
            EmpRelatedInfoRespDTO empRelatedInfoRespDTO = JSON.parseObject(empRelatedInfoRespStr, EmpRelatedInfoRespDTO.class);
            try {
                employeeSyncService.syncData(empRelatedInfoRespDTO);
            } catch (BizException e) {
                log.error("人员相关数据-业务异常，error is :", e);
            }
        }
    }

}
